package com.link.use;

import com.sun.jdi.VoidType;

import java.util.Stack;

public class links {
    public static void main(String[] args) {
        herosx bob = new herosx(1, "bob");
        herosx zd = new herosx(1, "zd");
        herosx siry = new herosx(2, "siry");
        herosx kufu = new herosx(3, "kufu");
        herosx vivo = new herosx(4, "vivo");
        herosx oppo = new herosx(4,"oppo");
        herosx bob1 = new herosx(1, "bob");
        herosx zd1 = new herosx(5, "zd");
        herosx siry1 = new herosx(7, "siry");
        herosx kufu1 = new herosx(8, "kufu");
        herosx vivo1 = new herosx(9, "vivo");
        herosx oppo1 = new herosx(10,"oppo");
        linkset link = new linkset();
        link.add(bob);
        link.add(siry);
        link.add(kufu);
        link.add(vivo);
        link.gh(oppo);
        link.del(1);
        link.add(bob1);
        link.bl();
        int c=link.cd();
        System.out.println(c);
//        link.fxdw(2);
//        link.dftg();
//        link.bl();
        link.fxprint();

        link.bl();
    }
}

class linkset{
    private herosx head=new herosx(0," ");
    private herosx head1=new herosx(0," ");
    boolean flag=false;

    public void fxprint(){
        Stack<herosx>stack=new Stack();
        herosx cur=head;
        while (cur!=null){
            stack.add(cur);
            cur=cur.next;
        }
        while(stack.size()>0){
            System.out.println(stack.pop());
        }
    }
    public void fxdw(int num){
        herosx temp=head;
        if(head==null){
            System.out.println("链表为空");
            return;
        }
        for (int i = 0; i <cd()-num+1 ; i++) {
            temp=temp.next;
        }
        System.out.println(temp.num+"\t"+temp.name);
    }
    public int cd(){
        herosx temp=head;
        if(head==null){
            return 0;
        }
        int a=0;
        while (temp.next!=null){
            a+=1;
            temp=temp.next;

        }
        return a;
    }

    public void dftg(){
        herosx headh = new herosx(0, " ");
        herosx temp=head.next;
        herosx next=null;
        if(head==null){
            return;
        }
        while(temp!=null){
            next=temp.next;
          temp.next=headh.next;
            headh.next=temp;
            temp=next;
         //  temp= temp.next;
        }
        head=headh;
    }
    public void add(herosx man){
        herosx temp=head;
        boolean flag=false;
        while (true){
            if(temp.next==null){
                break;
            }
            if(temp.next.num>man.num){
                break;
            }
            else if(temp.next.num==man.num){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if(flag){
            System.out.println("位置已被占");
        }
        else {
            man.next=temp.next;
            temp.next=man;
        }
    }
    public void add1(herosx man){
        herosx temp=head;
        boolean flag=false;
        while (true){
            if(temp.next==null){
                break;
            }
            if(temp.next.num>man.num){
                break;
            }
            else if(temp.next.num==man.num){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if(flag){
            System.out.println("位置已被占");
        }
        else {
            man.next=temp.next;
            temp.next=man;
        }
    }
    public void del(int num){

        boolean flag=false;
        herosx temp= head;
        if(temp==null){
            System.out.println("链表为空");
            return;

        }
        while(true){
            if(temp.next==null){
                break;
            }
            if(temp.next.num==num){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if(flag){
            temp.next=temp.next.next;
        }
        else{
            System.out.println("你输入的值未查询到");
        }
    }
    public void gh(herosx man){
        boolean flag=false;
        herosx temp=head;
        if(temp==null){
            System.out.println("链表为空");
            return;

        }
        while(true){
            if(temp==null){
                break;
            }
            if(temp.num==man.num){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if(flag){
            temp.name=man.name;
        }
        else{
            System.out.println("你输入的值不存在");
        }
    }
    public void bl(){
        herosx temp=head.next;
        if(temp==null){
            System.out.println("链表为空");
            return;

        }

        while (true){
            if(temp==null){
                break;
            }
            System.out.println(temp.num+"  "+temp.name+"\n");
            temp=temp.next;
        }
    }

}
class herosx{
    public int num;
    public String name;
    public herosx next;

    public herosx(int num, String name) {
        this.num = num;
        this.name = name;
    }
    public String prin(){
        return "hero[num="+num+",name="+name+"]";
    }
}
